// pages/category/category.js
const {
    PostGoodsList,
    GetClassify,
    getShopping,
    GetDetail,
    getduijie,
    PostPrice
} = require('../../http/api')
import {
    Fnicons
} from '../../utils/auth'
Page({
    /**
     * 页面的初始数据
     */
    data: {
        value: '', //搜索值
        sidebar: [], //导航栏
        ListData: [], //商品数据
        activeKey: 0, //选中项的索引
        myId: 322219, //筛选id
        myData: [],
        show: false, //弹框位置
        properData: [], //弹框规格总数据
        proper: [], //弹框规格数据
        DuiData: [], //对接
        childId: [], //规格id集合
        idAll: [], //父子ID
        numll: 1, //步进器
        goodsId: '',
        reveId: [],
        idaa: [],
        GoodsPrice: 0, //点击规格显示不同的价格
    },
    //搜索回车事件
    inp(e) {
        this.setData({
            value: e.detail
        })
        wx.navigateTo({
            url: `/pages/goods/list?name=${this.data.value}`,
        })
    },
    list(e) {
        let id = e.target.dataset.stores
        let data = this.data.ListData.map(v => v.id == id)
        console.log(data);
    },
    //列表跳详情
    list(e) {
        // console.log(e.target.dataset.id);
        let id = e.target.dataset.id
        console.log(id);
        wx.navigateTo({
            url: `/pages/goods-details/index?id=${id}`,
        })
    },
    //点击筛选数据
    filter(e) {
        const {
            id
        } = e.currentTarget.dataset
        console.log(id);
        this.setData({
            myId: id
        })
        // 对应的数据id
        let uu = this.data.ListData.filter(ele => ele.categoryId == this.data.myId)
        this.setData({
            myData: uu
        })
    },
    //加入购物车
    addCart(e) {
        let {
            id
        } = e.currentTarget.dataset
        console.log(id);
        let token = wx.getStorageSync('token')
        let obj = {
            goodsId: id,
            number: 1,
            token
        }
        console.log(obj);
        getShopping(obj).then(res => {
            Fnicons()
            console.log(res);
            if (res.code == 0) {
                wx.showToast({
                    title: '加入购物车成功',
                })
            } else {
                wx.showToast({
                    title: '加入购物车失败',
                })
            }

        })
    },
    //加号加入购物车
    async add(e) {
        //底部taber隐藏
        wx.hideTabBar({
            animation: true,
        })
        this.setData({
            show: true
        });
        let {
            id
        } = e.target.dataset
        this.setData({
            goodsId: id
        })
        const res = await GetDetail(id)
        const res2 = await getduijie({
            goodsId: id
        })
        console.log(res);
        // console.log(res2);
        this.setData({
            properData: res.data,
            proper: res.data.properties,
            DuiData: res2.data
        })

    },
    //点击规格
    chil(e) {
        const {
            id,
            goodsid
        } = e.currentTarget.dataset
        console.log(e);
        const token = wx.getStorageSync('token')
        this.data.proper.forEach(ele => {
            ele.childsCurGoods.forEach(item => {
                if (item.id == id) {
                    item.status = !item.status
                    let all = {
                        optionId: item.propertyId,
                        optionValueId: item.id
                    }
                    this.data.idAll.push(all)
                    let obj = item.propertyId + ":" + item.id
                    let inof = this.data.reveId.indexOf(obj)
                    console.log(inof);
                    if (inof == -1) {
                        this.data.reveId.push(obj)
                        this.data.reveId.reverse()
                        this.setData({
                            reveId: this.data.reveId
                        })
                    }else{
                        this.data.reveId.splice(inof,1)
                        this.setData({
                            reveId: this.data.reveId
                        })
                    }
                    let GoodsPrice = {
                        propertyChildIds: this.data.reveId,
                        token,
                        goodsId: this.data.goodsId
                    }
                    PostPrice(GoodsPrice).then(res => {
                        // console.log(res);
                        this.setData({
                            GoodsPrice: res.data.price
                        })
                    })
                    // console.log(this.data.properData);
                    this.setData({
                        idAll: this.data.idAll
                    })
                    // console.log(this.data.idAll);
                    // console.log(this.data.properData);
                }
            })
        })
        this.setData({
            proper: this.data.proper
        })
    },
    // 步进器
    onChange(e) {
        this.setData({
            numll: e.detail
        })
    },
    // 加入购物车
    async addGoods(e) {

        let token = wx.getStorageSync('token')
        let obj = {
            token,
            sku: JSON.stringify(this.data.idAll),
            number: this.data.numll,
            goodsId: this.data.goodsId
        }
        console.log(e);
        const res = await getShopping(obj)
        Fnicons()
        console.log(res);
        if (res.code == 0) {
            wx.showToast({
                title: '加入购物车成功',
            })
            // 关闭模态框
            this.setData({
                show: false
            })
            wx.showTabBar({
                animation: true,
            })
            Fnicons()
        } else {
            wx.showToast({
                title: '加入购物车失败',
                icon: 'error'
            })
        }
        //加入购物车清空suk数据
        this.setData({
            idAll: []
        })
    },
    //关闭规格弹框
    onClose() {
        wx.showTabBar({
            animation: true,
        })
        this.setData({
            show: false
        });

    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        if (options.id) {
            const {
                id,
                index
            } = options
            this.setData({
                activeKey: index,
                myId: id
            })
        }



    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
        //商品列表
        PostGoodsList().then(res => {
            console.log(res);
            let num = res.data.result
            let ListData = num.reverse()
            this.setData({
                ListData
            })
            // 对应的数据id
            let uu = this.data.ListData.filter(ele => ele.categoryId == this.data.myId)
            this.setData({
                myData: uu
            })
        })
        //导航栏
        GetClassify().then(res => {
            console.log(res);
            this.setData({
                sidebar: res.data
            })
        })
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})